package com.geostar.constructionland.statistics.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.geostar.constructionland.common.entity.statistics.StatisticsProfile;
import com.geostar.constructionland.statistics.mapper.StaticProfileMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * 统计配置表操作服务
 *
 * @Author zhu_jiacheng
 * @Date:2020/7/15 12:40
 */
@Service
public class StatisticsProfileService extends ServiceImpl<StaticProfileMapper, StatisticsProfile> {

    @Autowired
    private StaticProfileMapper staticProfileMapper;

    // 根据分析类型和业务id获取一组操作配置类
    public List<StatisticsProfile> getStatisticsProfile(String uniqueTaskId) {
        return staticProfileMapper.selectList(new LambdaQueryWrapper<StatisticsProfile>().eq(StatisticsProfile::getUniqueTaskId, uniqueTaskId));
    }

    public IPage<StatisticsProfile> getStatisticProfilesForPage(int pageNo, int pageNum, String query_param_statistics_name) {
        IPage<StatisticsProfile> analyseVersionIPage = new Page<>(pageNo, pageNum);
        QueryWrapper<StatisticsProfile> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(query_param_statistics_name)) {
            queryWrapper.like("statistics_name", query_param_statistics_name);
        }
        queryWrapper.orderByDesc("modified_time");
        return staticProfileMapper.selectPage(analyseVersionIPage, queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    public void deleteById(String id) {
        if (id.contains(",")) {
            // 将多个id处理成集合形式
            List<String> versionIds = Arrays.asList(id.split(","));
            staticProfileMapper.deleteBatchIds(versionIds);
        } else {
            staticProfileMapper.deleteById(id);
        }
    }
}
